/*
 * @Author: your name
 * @Date: 2021-04-21 14:59:55
 * @LastEditTime: 2021-04-21 16:52:28
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \provider_demo\lib\provider_demo.dart
 */

import 'package:flutter/material.dart';
import 'counter_model.dart';
import 'package:provider/provider.dart';
import 'test_icon.dart';

class ProviderTabPage1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final _counter = Provider.of<CounterModel>(context);
    final textSize = Provider.of<double>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('First Page'),
      ),
      body: Center(
        child: Text(
          'Counter: ${_counter.counter}',
          style: TextStyle(fontSize: textSize),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => ProviderTabPage2())),
        child: Icon(Icons.navigate_next),
      ),
    );
  }
}

class ProviderTabPage2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 使用Provider.of 获取资源，可以得到资源暴露的数据的读写接口
    // 注意滥用Provider.of方法，会导致当数据更新时，页面的其他的子widget也会跟着刷新，造成性能问题
    // Provider.of 方法来获取资源数据。而如果我们想写数据，则需要通过获取到的资源数据，调用其暴露的更新数据方法
    final _counter = Provider.of<CounterModel>(context);
    final textSize = Provider.of<double>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Second Page'),
      ),
      body: Text(
        'Counter: ${_counter.counter}',
        style: TextStyle(fontSize: textSize),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _counter.increment,
        child: TestIcon(),
      ),
    );
  }
}
